﻿using System;
using Microsoft.Phone.Tasks;
using AVICFEEDSApp.BingMapsGeocodeService;

namespace AVICFEEDSApp
{
	internal static class Common
	{
		/// <summary>
		/// Saves a contact.
		/// </summary>
		/// <param name="saveContactTask">A SaveContactTask object to use when saving the contact.</param>
		/// <param name="name">The name of the contact</param>
		/// <param name="longitude">Longitude of the contact</param>
		/// <param name="latitude">Latitude of the contact</param>
		internal static void SaveContact(SaveContactTask saveContactTask, string name, double longitude, double latitude)
		{
			Common.SaveContact(saveContactTask, name, longitude, latitude, "#", null);
		}

		/// <summary>
		/// Saves a contact.
		/// </summary>
		/// <param name="saveContactTask">A SaveContactTask object to use when saving the contact.</param>
		/// <param name="name">The name of the contact</param>
		/// <param name="longitude">Longitude of the contact</param>
		/// <param name="latitude">Latitude of the contact</param>
		/// <param name="address">Address of the contact</param>
		internal static void SaveContact(SaveContactTask saveContactTask, string name, double longitude, double latitude, Address address)
		{
			Common.SaveContact(saveContactTask, name, longitude, latitude, "#", address);
		}

		/// <summary>
		/// Saves a contact.
		/// </summary>
		/// <param name="saveContactTask">A SaveContactTask object to use when saving the contact.</param>
		/// <param name="name">The name of the contact</param>
		/// <param name="longitude">Longitude of the contact</param>
		/// <param name="latitude">Latitude of the contact</param>
		/// <param name="phoneNumber">Phone number of the contact</param>
		/// <param name="address">Address of the contact</param>
		internal static void SaveContact(SaveContactTask saveContactTask, string name, double longitude, double latitude, string phoneNumber, Address address)
		{
			String firstName = AVICDecoderRing.Encode(longitude, latitude);
			//int maxChars = 40 - firstName.Length - 1;
			firstName = String.Format("{0} {1}", name.Length > 20 ? name.Substring(0, 20) : name, firstName);

			saveContactTask.FirstName = firstName;
			saveContactTask.LastName = "";
			saveContactTask.WorkPhone = phoneNumber;
			saveContactTask.Notes = name;

			if (address != null)
			{
				saveContactTask.WorkAddressCity = address.Locality;
				saveContactTask.WorkAddressCountry = address.CountryRegion;
				saveContactTask.WorkAddressState = address.AdminDistrict;
				saveContactTask.WorkAddressStreet = address.AddressLine;
				saveContactTask.WorkAddressZipCode = address.PostalCode;
			}

			saveContactTask.Show();
		}
	}
}
